package push_modules

import (
	"crypto/sha1"
	"encoding/hex"
	"errors"
	"fmt"
	"hft/e"
	"hft/global"
	"hft/tools"
	"hft/tools/weixin/model"
	"sort"
	"strings"
)

type Check struct{}

// 加密
func sha1Encrypt(input string) string {
	// 将输入字符串转换为字节数组
	data := []byte(input)

	// 创建SHA1哈希对象
	hash := sha1.New()

	// 计算哈希值
	hash.Write(data)

	// 获取哈希值的字节数组
	encrypted := hash.Sum(nil)

	// 将字节数组转换为十六进制字符串
	encryptedString := hex.EncodeToString(encrypted)

	return encryptedString
}

// 校验token 接收微信推送
func (*Check) CheckToken(data *model.Check) (str string, err error) {

	strs := []string{global.OC_CONFIG.WeiXin.Token, data.Timestamp, data.Nonce}

	sort.Strings(strs)
	fmt.Println("排序", strs)

	newStr := sha1Encrypt(strings.Join(strs, ""))

	fmt.Println(newStr, data.Signature, tools.JsonParse(data))

	if newStr != data.Signature {
		return "", errors.New(e.ValidError)
	}

	return data.EchoStr, nil
}

var WeiXinCheck = new(Check)
